package org.netty.push.server.message.handler;

import org.netty.push.appinfo.service.AppInfoService;
import org.netty.push.core.proto.ConnectRequestMsg;
import org.netty.push.core.proto.ConnectResponseMsg;
import org.netty.push.core.proto.handler.MessageHandler;
import org.netty.push.server.channel.ChannelHolderUtlil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import io.netty.channel.ChannelHandlerContext;

@Component
public class ConnectRequestHandler implements MessageHandler<ConnectRequestMsg> {
	Logger logger=LoggerFactory.getLogger(ConnectRequestHandler.class);
	
	@Autowired
	private AppInfoService appInfoService;
	
	public ConnectRequestHandler( AppInfoService appInfoService ){
		this.appInfoService=appInfoService;
	}
	
	@Override
	public void handle(ChannelHandlerContext ctx, ConnectRequestMsg msg) {
		logger.debug("ConnectRequestMsg is {} ",msg);
		ConnectResponseMsg rs = appInfoService.doConnected(msg);
		ctx.writeAndFlush(rs);
		ChannelHolderUtlil.put(rs.getDeviceToken(), ctx.channel());
	}

	@Override
	public boolean supported(Object msg) {
		return ConnectRequestMsg.class.isAssignableFrom(msg.getClass());
	}


}
